package com.sysmanage.etl.service.impl;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sysmanage.etl.dao.ColumnDao;
import com.sysmanage.etl.dao.PropertyColumnDao;
import com.sysmanage.etl.dao.PropertyDao;
import com.sysmanage.etl.dao.RuleDao;
import com.sysmanage.etl.dao.TableDao;
import com.sysmanage.etl.model.ETL_Column;
import com.sysmanage.etl.model.ETL_Property;
import com.sysmanage.etl.model.ETL_Property_Column;
import com.sysmanage.etl.model.ETL_Rule;
import com.sysmanage.etl.model.ETL_Table;
import com.sysmanage.etl.service.IRuleService;
import com.sysmanage.etl.vo.MappedVO;
import com.sysmanage.framework.dao.hspring.DAOInterface;
import com.sysmanage.framework.service.BaseService;

@Service
@Transactional
public class RuleServiceImpl extends BaseService<ETL_Rule> implements IRuleService {
	@Resource
	private RuleDao ruleDao;
	@Resource
	private TableDao tableDao;
	@Resource
	private ColumnDao columnDao;
	@Resource
	private PropertyDao propertyDao;
	@Resource
	private PropertyColumnDao propertyColumnDao;

	
	@Override
	public Integer save(List<MappedVO> mappedVOs) {
		ETL_Rule rule = null;
		ETL_Property returnProperty = null;
		for (MappedVO vo : mappedVOs) {
			if (returnProperty == null) {
				returnProperty = propertyDao.findById(vo.getRulereturn());
			}
			ETL_Property property = propertyDao.findById(vo.getPid());
			ETL_Column column = columnDao.findById(vo.getCid());
			ETL_Table table = property.getTable();
			if (rule == null) {
				rule = ruleDao.find("from ETL_Rule e where e.table.table_id = " + table.getTable_id() + " and e.shop_id = " + vo.getShopid() + " and e.rule_index = " + vo.getRuleindex());
				if (rule == null) {
					rule = new ETL_Rule();
					rule.setRule_index(vo.getRuleindex());
					rule.setRule_type(vo.getRuletype());
					rule.setTable(table);
					rule.setShop_id(vo.getShopid());
					rule.setRule_type(vo.getRuletype());
					rule.setRule_return_property(returnProperty);
					ruleDao.save(rule);
				} else {
					rule.setRule_index(vo.getRuleindex());
					rule.setRule_type(vo.getRuletype());
					rule.setTable(table);
					rule.setShop_id(vo.getShopid());
					rule.setRule_type(vo.getRuletype());
					rule.setRule_return_property(returnProperty);
					ruleDao.alter(rule);
				}
			}
			
			Set<ETL_Property_Column> pcs = rule.getPropertyColumns();
			
			if (pcs != null) {
				for (ETL_Property_Column pc : pcs) {
					propertyColumnDao.delete(pc);
				}
			}
			
			ETL_Property_Column pc = new ETL_Property_Column();
			pc = new ETL_Property_Column();
			pc.setColumn(column);
			pc.setProperty(property);
			pc.setRule(rule);
			pc.setIsForeignKey(vo.getIsForeignkey() == null || vo.getIsForeignkey() != 1 ? false : true);
			pc.setIsPrimaryKey(vo.getIsPrimarykey() == null || vo.getIsPrimarykey() != 1 ? false : true);
			pc.setIsRepeat(vo.getIsRepeat() == null || vo.getIsRepeat() != 1 ? false : true);
			pc.setPc_index(vo.getForeignkey());
			pc.setPrimaryKey(vo.getPrimarykey());
			pc.setOtherValue(vo.getOtherValue());
			propertyColumnDao.save(pc);
		}
		return rule.getRule_id();
	}


	@Override
	public boolean deleteRule(ETL_Rule rule) {
		for (ETL_Property_Column pc : rule.getPropertyColumns() ) {
			propertyColumnDao.delete(pc);
		}
		ruleDao.delete(rule);
		return true;
	}

	@Override
	public boolean hasRule(Integer shop_id) {
		int count = ruleDao.findCountBySql("from ETL_Rule where shop_id = " + shop_id);
		return count > 0 ? true : false;
	}


	@Override
	protected DAOInterface<ETL_Rule> getDAO() {
		return this.ruleDao;
	}
	
}
